package LeetCode;

import java.util.Arrays;
import java.util.zip.CheckedOutputStream;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}23:25
 */
public class LeetCode322 {
    private static int res = Integer.MAX_VALUE;
    public static void main(String[] args) {
        int[] nums = {1,2,3};
        int amount = 4;
        System.out.println(new LeetCode322().coinChange3(nums, amount));
//        new LeetCode322().dfs(nums,amount,0);
//        if (res == 0){
//            System.out.println(-1);
//        }else {
//            System.out.println(res);
//        }
    }

        public int coinChange(int[] coins, int amount) {
            int max = amount + 1;
            int[] dp = new int[amount + 1];
            Arrays.fill(dp, max);
            dp[0] = 0;
            for (int i = 1; i <= amount; i++) {
                for (int j = 0; j < coins.length; j++) {
                    if (coins[j] <= i) {
                        dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                    }
                }
            }
            return dp[amount] > amount ? -1 : dp[amount];
        }


        // 递归法
    public void dfs(int[] nums,int total,int count){
        if (total < 0 || count == total){
            return;
        }
        if (total == 0){
            res = Math.min(count,res);
        }


        for (int i = 0; i < nums.length; i++){
            dfs(nums,total - nums[i],count + 1);
        }
    }
    // 动态规划解法

    public int coinChange2(int[] coins, int amount) {
        // 给 0 占位
        int[] dp = new int[amount + 1];

        // 注意：因为要比较的是最小值，这个不可能的值就得赋值成为一个最大值
        Arrays.fill(dp, amount + 1);

        dp[0] = 0;

        for (int i = 1; i <= amount; i++) {
            for (int coin : coins) {
                if (i - coin >= 0 && dp[i - coin] != amount + 1) {
                    dp[i] = Math.min(dp[i], 1 + dp[i - coin]);
                }
            }
        }

        if (dp[amount] == amount + 1) {
            dp[amount] = -1;
        }
        return dp[amount];
    }

    // 完全背包问题
    public int coinChange3(int[] coins, int amount) {
        int[] dp = new int[amount + 1];
        Arrays.fill(dp,amount + 1);
        dp[0] = 0;
        for (int coin : coins) {
            for (int i = coin; i <= amount; i++){
                dp[i] = Math.min(dp[i],dp[i - coin] + 1);//这里直接赋值的是 最小的投币个数
            }
        }
        return dp[amount];
    }
}
